/**
 * 安全奖i订单 列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-02 11:28:06
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-incentiveGoodsOrder'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#incentiveGoodsOrder-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    laydate.render({
        elem: '#applyStartDate'
    });
    laydate.render({
        elem: '#applyEndDate'
    });
    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'incentiveGoodsOrderTable',
            url: ctx + 'incentiveGoodsOrder/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'checkbox'},
                {field: 'goodsOrderId', title: '主键', minWidth: 120, hide: true},
                {field: 'tenantName', title: '公司名称', minWidth: 120},
                {
                    field: 'orderNo',
                    title: '订单编号',
                    width: 200,
                    align: 'center',
                    event: 'detail',
                    templet:function (d) {
                        return '<p style="color:#01aaed;cursor:pointer">'+d.orderNo+'</p>'
                    }
                },
                {
                    field: 'goodsOrderType',
                    title: '订单类型',
                    width: 130,
                    align: 'center',
                    templet: function (d) {
                        if (d.goodsOrderType === "1") {
                            return  '<span class="layui-badge febs-tag-green">安全奖励</span>';
                        } else if (d.goodsOrderType === "2") {
                            return  '<span class="layui-badge febs-tag-blue">线上申请</span>';
                        } else if (d.goodsOrderType === "3") {
                            return  '<span class="layui-badge febs-tag-red">线下兑换</span>';
                        } else {
                            return "";
                        }
                    }
                },
                {
                    field: 'userList',
                    title: '兑现用户',
                    minWidth: 120,
                    templet: function (d) {
                        if (d.userList && d.userList.length > 0) {
                            let userList = d.userList;
                            let html = "";
                            for (let i = 0; i < userList.length; i++) {
                                if (i !== 0) {
                                    html += '、'
                                }
                                html += userList[i].realName + ' (' + userList[i].userNo + ' / ' + userList[i].deptName + ')';
                            }
                            return html;
                        } else {
                            return "--"
                        }
                    }
                },
                {field: 'rewardApplyId', title: '安全奖励申请Id', minWidth: 120, hide: true},
                {
                    field: 'orderDate',
                    title: '订单时间',
                    width: 150,
                    align: "center",
                    templet: function (d) {
                        if (d.orderDate) {
                            return d.orderDate.substring(0, 10)
                        }
                    }
                },
                {
                    field: 'orderScore',
                    title: '订单积分',
                    width: 120,
                    align: "center",
                    templet: function (d) {
                        if (d.goodsOrderType === "1") {
                            return "--";
                        } else {
                            return d.orderScore;
                        }
                    }
                },
                {
                    field: 'exchangeDate',
                    title: '兑换时间',
                    width: 150,
                    align: "center",
                    templet: function (d) {
                        if (d.exchangeDate) {
                            return d.exchangeDate.substring(0, 10)
                        } else {
                            return "--"
                        }
                    }
                },
                {
                    field: 'status',
                    title: '订单状态',
                    width: 120,
                    align: "center",
                    templet: function (d) {
                        if (d.status === "1") {
                            return  '<span class="layui-badge febs-tag-geekblue">待提交</span>';
                        } else if (d.status === "2") {
                            return  '<span class="layui-badge febs-tag-volcano">待领取</span>';
                        } else if (d.status === "3") {
                            return  '<span class="layui-badge febs-tag-cyan">已领取</span>';
                        } else if (d.status === "4") {
                            return  '<span class="layui-badge febs-tag-green">已取消</span>';
                        } else {
                            return "--"
                        }
                    }
                },
                {title: '操作', toolbar: '#incentiveGoodsOrder-tool', align: 'center', width: 150, fixed: 'right'}
            ]],
            toolbar: '#incentiveGoodsOrder-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(incentiveGoodsOrderTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(incentiveGoodsOrderTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('线下兑现', 'incentiveGoodsOrder/add', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '65%' : '70%', '60%'],
                yes: function (index, layero) {
                    $('#incentiveGoodsOrder-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                // TODO 需要改
                data: {tenantId: $view.find('select[name=tenantId]').val()}
            });
        }
    });


    table.on('tool(incentiveGoodsOrderTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.goodsOrderId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'incentiveGoodsOrder/update/' + data.goodsOrderId, {
                area: [$(window).width() <= 750 ? '50%' : '40%', '50%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#incentiveGoodsOrder-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {},
            });
        }
        if (layEvent === 'handOut') {
            febs.modal.open('兑现发放', 'incentiveGoodsOrder/handOut/' + data.goodsOrderId, {
                area: [$(window).width() <= 750 ? '50%' : '50%', '60%'],
                btn: data.goodsOrderType === "2" ? ['确认兑现', '取消兑现','取消'] : ['确认兑现', '取消'],
                yes: function (index, layero) {
                    $('#incentiveGoodsOrder-handOut').find('#submit').trigger('click');
                },
                btn2: function () {
                    if(data.goodsOrderType === "2"){
                        // 取消兑现
                        $('#incentiveGoodsOrder-handOut').find('#cancel').trigger('click');
                        return false;
                    }
                    layer.closeAll();
                },
                btn3: function () {
                    layer.closeAll();
                },
                data: {},
            });
        }
        if (layEvent === 'detail') {
            febs.modal.open('安全奖品发放详情', 'incentiveGoodsOrder/detail/' + data.goodsOrderId, {
                area: $(window).width() <= 750 ? '90%' : ['1200px', '700px'],
                shadeClose: true,
                btn: ['取消'],
                yes: function (index, layero) {
                    layer.close(index)
                },
            });
        }
    });

    // $delete.on('click', function () {
    //     var checkStatus = table.checkStatus('incentiveGoodsOrderTable');
    //     if (!checkStatus.data.length) {
    //         febs.alert.warn('没有选择要删除的数据！');
    //     } else {
    //         febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
    //             var ids = [];
    //             layui.each(checkStatus.data, function (key, item) {
    //                 ids.push(item.goodsOrderId);
    //             });
    //             deleteIds(ids.join(','));
    //         });
    //     }
    // });

    function getQueryParams() {
        return {
            tenantId: $searchForm.find('select[name="tenantId"]').val(),
            orderNo: $searchForm.find('input[name="orderNo"]').val()?.trim(),
            applyStartDate: $searchForm.find('input[name="applyStartDate"]').val()?.trim(),
            applyEndDate: $searchForm.find('input[name="applyEndDate"]').val()?.trim(),
            goodsOrderType: $searchForm.find('select[name="goodsOrderType"]').val(),
            status: $searchForm.find('select[name="status"]').val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'incentiveGoodsOrder/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});